package com.jystudio.roc;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.jystudio.dingTalk.DingTalkService;
import com.jystudio.dingTalk.DingTalkSign;
import com.jystudio.roc.bean.RocDTO;
import com.jystudio.roc.utils.RocUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling
@Slf4j
public class Application {

    private boolean g_lately = false;


    @Scheduled(fixedDelay = 1000 * 60 * 5L)
    public void rocHour() {
        DingTalkSign dingTalkSign = new DingTalkSign();
        long time = 1000 * 60 * 60 * 24 * 7L;
        try {
            Thread.sleep(Math.round(Math.random() * 1000 * 60) + (g_lately ? 1000 * 60 * 60 : 0));
        } catch (InterruptedException e) {

        }
        dingTalkSign.setDingTalkWebHook("https://oapi.dingtalk.com/robot/send?access_token=39033c2fc0e717c265fc9d3347ea6421785806730bd845df3609a3ce734ee7bb");
        dingTalkSign.setDingTalkSing("SEC8b36137c65dc0fec2a6758f28596f21db0e8991864edece0a6eb06f31bae7189");
        List<RocDTO> list = new ArrayList<>();
        RocUtils.getBaoan(1).forEach(map -> {
            //7天内重复推送
            if (System.currentTimeMillis() - map.getCreatedAt().getTime() < time) {
                list.add(map);
            } else {
                log.info("item-baoan-out:{},{},{},{}", map.getDate(), System.currentTimeMillis() - map.getCreatedAt().getTime(), time, JSONObject.toJSONString(map));
            }
        });
        RocUtils.securityHousing("http://zjj.sz.gov.cn/ztfw/zfbz/tzgg2017/index.html").forEach(map -> {
            if (System.currentTimeMillis() - map.getCreatedAt().getTime() < time) {
                list.add(map);
            } else {
                log.info("item-roc-out:{},{},{},{}", map.getDate(), System.currentTimeMillis() - map.getCreatedAt().getTime(), time, JSONObject.toJSONString(map));
            }
        });
        g_lately = list.size() > 0;
        if (list.size() > 0) {
            log.info("map:{}",JSONObject.toJSONString(list));
            try {
                Map<String, Object> map = new HashMap<>();
                map.put("mapList", list);
                OapiRobotSendResponse oapiRobotSendResponse = DingTalkService.sendMsg(map, "roc.md", "深圳公租房相关信息", dingTalkSign);
            } catch (Exception e) {
                log.error("send msg error:{}",e);
            }

        }
        log.error("rocHour:{}", g_lately);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
